package com.bto.solar.work.service.biz_stage_handler;

import com.bto.solar.work.common.BizException;
import com.bto.solar.work.constant.BizStageEnum;
import com.bto.solar.work.constant.BizTaskEnum;
import com.bto.solar.work.constant.BizTaskStateEnum;
import com.bto.solar.work.constant.Constants;
import com.bto.solar.work.constant.DBConstants;
import com.bto.solar.work.dao.IOrderBaseService;
import com.bto.solar.work.entity.OrderBase;
import com.bto.solar.work.lease.LeaseApi;
import com.bto.solar.work.service.BtoCompanyService;
import com.bto.solar.work.service.BtoLeaseReviewService;
import com.bto.solar.work.service.BtoOrderStateService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 王小波
 * @description: 购售电合同阶段处理器
 * @date 2023/9/25 13:53
 */

@Component
public class GSContractStageHandler implements StageHandler {
    private final String stageId = BizStageEnum.GS_CONTRACT.getStageId();
    private final String taskId = BizTaskEnum.TASK_GSDHTXX.getTaskId();

    @Autowired
    private IOrderBaseService orderService;
    @Autowired
    private BtoCompanyService btoCompanyService;
    @Autowired
    private BtoOrderStateService btoOrderStateService;
    @Autowired
    private BtoLeaseReviewService btoLeaseReviewService;
    @Autowired
    private LeaseApi leaseApi;


    //购售电合同
    @Override
    public void createNewInitState(String orderId, String stageId, String taskId) {
        btoOrderStateService.createOrderState(orderId, this.stageId, this.taskId,
                BizTaskStateEnum.WAITING_FILLED.getStateId());
    }

    //购售电合同待-越秀审批
    @Transactional
    @Override
    public void updateToWaitingApprovalState(String orderId, String stageId, String taskId) {
        btoOrderStateService.updateOrSaveOrderState(orderId, this.stageId, this.taskId,
                BizTaskStateEnum.WAITING_APPROVAL_BTO.getStateId(),
                BizTaskStateEnum.WAITING_FILLED.getStateId(),
                BizTaskStateEnum.APPROVAL_REJECT_BTO.getStateId(),
                BizTaskStateEnum.APPROVAL_REJECT_LEASE.getStateId());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateToApprovalPassState(String orderId, String stageId, String taskId, String approvalType) {
        if (Constants.APPROVAL_BTO.equals(approvalType)) {
            OrderBase orderBase = orderService.getById(orderId);
            /**
            *   购售电合同内审通过时：合同已经、或者未起租
             *   起租：直接推送
             *   未起租：等待起租，推送
            */
            if (btoLeaseReviewService.isOrderIncept(orderId)) {
                //购售电合同推送越秀
                boolean pushLease = leaseApi.powerSellOlFileContractPushLease(orderBase);
                if (!pushLease) {
                    throw new BizException("订单{} 购售电合同推送资方失败，请重试", orderId);
                }
                btoOrderStateService.updateOrSaveOrderState(orderId, this.stageId, this.taskId,
                        BizTaskStateEnum.WAITING_APPROVAL_LEASE.getStateId(),
                        BizTaskStateEnum.WAITING_APPROVAL_BTO.getStateId(), true);
            }
            else {
                btoOrderStateService.updateOrSaveOrderState(orderId, this.stageId, this.taskId,
                        BizTaskStateEnum.INCEPT_WAITING_PUSH_GSDHT.getStateId(),
                        BizTaskStateEnum.WAITING_APPROVAL_BTO.getStateId(), true);
            }


            val company = btoCompanyService.getCompanyById(orderBase.getCompanyId());
            String companyType = company.getType();
            //代理商 -- 无需等待越秀审核，开启资料结算
            if (DBConstants.COMPANY_TYPE_AGENT.equals(companyType)) {
                btoOrderStateService.createOrderState(orderId, BizStageEnum.SETTLEMENT.getStageId(),
                        BizTaskEnum.TASK_ZLJSXX.getTaskId(), BizTaskStateEnum.WAITING_FILLED.getStateId());
            }
            //非代理商
            if (!DBConstants.COMPANY_TYPE_AGENT.equals(companyType)) {
                updateOrderFinishedState(orderId);
            }
        } else if (Constants.APPROVAL_LEASE.equals(approvalType)) {
            btoOrderStateService.updateOrSaveOrderState(orderId, this.stageId, this.taskId,
                    BizTaskStateEnum.APPROVAL_PASS_LEASE.getStateId(),
                    BizTaskStateEnum.WAITING_APPROVAL_LEASE.getStateId(), true);
        }
    }

    @Override
    public void updateToApprovalRejectState(String orderId, String stageId, String taskId, String approvalType, Object rejectInfo) {
        if (Constants.APPROVAL_BTO.equals(approvalType)) {
            btoOrderStateService.updateOrSaveOrderState(orderId, this.stageId, this.taskId,
                    BizTaskStateEnum.APPROVAL_REJECT_BTO.getStateId(),
                    BizTaskStateEnum.WAITING_APPROVAL_BTO.getStateId(), true);
        } else if (Constants.APPROVAL_LEASE.equals(approvalType)) {
            btoOrderStateService.updateOrSaveOrderState(orderId, this.stageId, this.taskId,
                    BizTaskStateEnum.APPROVAL_REJECT_LEASE.getStateId(),
                    BizTaskStateEnum.APPROVAL_PASS_BTO.getStateId(), true);
        }


    }

    private void updateOrderFinishedState(String orderId) {
        btoOrderStateService.createOrderState(orderId, BizStageEnum.FINISHED.getStageId(), null, BizTaskStateEnum.FINISHED.getStateId());
    }


}
